//服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main()
{
	//创建套接字
	int soc_fd;
	soc_fd = socket(AF_INET, SOCK_STREAM, 0);
	if(-1 == soc_fd)
	{
		perror("socket failed!\n");
		return -1;
	}
	
	//确定网络结构体
	struct sockaddr_in jack;
	jack.sin_family = AF_INET;
	jack.sin_port = htons(3000);
	jack.sin_addr.s_addr = inet_addr("0.0.0.0");
	
	//将网络结构体与套接字绑定
	int bd;
	socklen_t len = sizeof(struct sockaddr);
	bd = bind(soc_fd, (struct sockaddr*)&jack, len);
	if(-1 == bd)
	{
		perror("bind failed!\n");
		return -1;
	}
	
	//监听客户端
	listen(soc_fd, 5);
	
	//等待客户端连接
	printf("等待客户端连接\n");
	int acc_fd;
	struct sockaddr_in rose;
	acc_fd = accept(soc_fd, (struct sockaddr*)&rose, &len);
	if(-1 == acc_fd)
	{
		perror("accept failed!\n");
		return -1;
	}
	
	//等待客户端发消息
	printf("等待客户端发消息\n");
	char buf[32];
	while(1)
	{
		read(acc_fd, buf, 32);
		printf("buf=%s",buf);
		printf("收到数据\n");
	}
	return 0;
}